package Thread;
//懒汉式
class Singletonlazy {
    private volatile static Singletonlazy instance = null;

    private static Object locker = new Object();

    public static Singletonlazy getInstance() {
        if (instance == null) {
            synchronized(locker) {
                if (instance == null) {
                    instance = new Singletonlazy();
                }
            }
        }
        return instance;
    }
    private Singletonlazy () {
        //防止反射攻击创建单例
    }
}
public class Demo2 {
    public static void main(String[] args) {
        Singletonlazy instance = Singletonlazy.getInstance();
        Singletonlazy instance2 = Singletonlazy.getInstance();
        System.out.println(instance2 == instance);
    }
}
